package cn.tedu.eguser1.base.exception;


import cn.tedu.eguser1.base.response.JsonResult;
import cn.tedu.eguser1.base.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doHandler(MethodArgumentNotValidException ex) {
        String data = ex.getBindingResult().getFieldError().getDefaultMessage();
        log.debug("MethodArgumentNotValidException：" + data);
        return new  JsonResult(StatusCode.OPERATION_FAILED,data);
    }
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doHandler(ConstraintViolationException ex) {
        String data = ex.getConstraintViolations().iterator().next().getMessage();
        log.debug("ConstraintViolationException：" + data);
        return new  JsonResult(StatusCode.OPERATION_FAILED,data);
    }

}
